<?php
/**
 * LÆ¡Ì�p luÃ¢n chuyÃªÌ‰n giÆ°Ìƒa csdl vaÌ€ object
 * LÆ¡Ì�p gÄƒÌ�n vÆ¡Ì�i bÄƒÌ‰ng id_zgallery_photo trong csdl 
 * @author nhat
 *
 */
class Zphoto extends ObjectModel 
{
	public $id;
	
	public $id_zgallery_photo;
	public $id_zgallery_album;
	public $name;
	public $description;
	public $freurl;
	public $image;
	public $nb_view=0;
	public $nb_like=0;
	public $limit_like=0;
	public $allow_like=1;
	public $random_lucky_member;
	public $active=1;
	public $date_add;
	
	public $table = 'zgallery_photo';
	
	
	protected $identifier = 'id_zgallery_photo';
	protected $fieldsRequiredLang = array('name');
	protected $fieldsRequired =array('image');
	protected $fieldsSizeLang = array('name' => 254,'description'=>10000);
	protected $fieldsValidateLang = array('name' => 'isCatalogName','description'=>'isString','freurl'=>'isLinkRewrite');
	protected $url;
	protected $allow;
	
	public function getFields ()
	{
		parent::validateFields ();
		$fields['id_zgallery_photo']=(int)($this->id);
		$fields['id_zgallery_album']=(int)($this->id_zgallery_album);
		$fields['image']=($this->image);
		$fields['nb_view']=($this->nb_view);
		$fields['nb_like']=($this->nb_like);
		$fields['limit_like']=($this->limit_like);
		$fields['allow_like']=(int)($this->allow_like);
		$fields['active']=(int)($this->active);
		$fields['date_add']=($this->date_add);
		$fields['random_lucky_member']=($this->random_lucky_member);
		return $fields;
	}
	
	public function __construct($id=NULL,$id_lang=NULL){
		$this->allow = (int)Configuration::get('PS_REWRITING_SETTINGS');
		$this->url = $_SERVER['SCRIPT_NAME'];
		parent::__construct ($id,$id_lang);
	}
	
	public function getTranslationsFieldsChild()
	{
		parent::validateFieldsLang();
		return parent::getTranslationsFields(array('name','description'));
	}
		
	static public function printNewsIconActive($id_pl_option, $tr)
	{
		$OptionClass = new Zphoto ($tr['id_zgallery_photo']);
		$id_zgallery_album = Tools::getValue('id_zgallery_album');
		echo 
		'<a href="index.php?tab=ZadminGallery&id_zgallery_album='.$id_zgallery_album.'&id_zgallery_photo='.(int)($OptionClass->id).'&changeActivephoto&token='.Tools::getAdminTokenLite('ZadminGallery').'">
			'.($OptionClass->active==1 ? '<img src="../img/admin/enabled.gif" />' : ( $OptionClass->active==0?'<img src="../img/admin/disabled.gif" />':'<img src="../img/admin/forbbiden.gif" />')).'
		</a>
		';
	}
	
	protected function makeTranslationFields(&$fields, &$fieldsArray, $id_language)
	{
		$fields[$id_language]['id_lang'] = $id_language;
		$fields[$id_language][$this->identifier] = (int)($this->id);
		foreach ($fieldsArray as $field)
		{
			/* Check fields validity */
			if (!Validate::isTableOrIdentifier($field))
				die(Tools::displayError());
			/* Copy the field, or the default language field if it's both required and empty */
			if ((!$this->id_lang AND isset($this->{$field}[$id_language]) AND !empty($this->{$field}[$id_language])) 
			OR ($this->id_lang AND isset($this->$field) AND !empty($this->$field)))
				$fields[$id_language][$field] = $this->id_lang ? pSQL($this->$field) : pSQL($this->{$field}[$id_language]);
			elseif (in_array($field, $this->fieldsRequiredLang))
				$fields[$id_language][$field] = $this->id_lang ? pSQL($this->$field) : pSQL($this->{$field}[Configuration::get('PS_LANG_DEFAULT')]);
			else
				$fields[$id_language][$field] = '';
		}
	}
	/** Get info photo when into album 
	 * 
	 * @param unknown_type $id_zgallery_album
	 * @param unknown_type $id_lang
	 * @param unknown_type $id_customer
	 * @param unknown_type $p
	 * @param unknown_type $n
	 * @param unknown_type $active
	 * @param unknown_type $sql_sort
	 * @param unknown_type $sql_limitfd
	 */
	static public function getListPhoto ($id_zgallery_album,$id_lang,$id_customer=0, $p=1, $n=false, $active = true,  $sql_sort = '',$sql_limit = '') {
		global $cookie,$link;
		$linkHome = $link->getPageLink('');
		$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS('SELECT * FROM '._DB_PREFIX_.'zgallery_photo a
					LEFT JOIN '._DB_PREFIX_.'zgallery_photo_lang b ON (a.id_zgallery_photo=b.id_zgallery_photo )
					WHERE 
						a.id_zgallery_album='.$id_zgallery_album.' AND b.`id_lang` = '.(int)($id_lang).'
						'.($active ? 'AND `active` = 1' : '').'
						'.($n ? 'LIMIT '.(int)(($p - 1) * $n).', '.(int)($n) : '') 
		);
		foreach ($result as $key => $value){
			if (Configuration::get('PS_REWRITING_SETTINGS')) {
				$result[$key]['link'] = Zphoto::getLinkUrlPhoto ($value['id_zgallery_album'],$value['id_zgallery_photo'] );;
			}else {
				$result[$key]['link'] = Tools::getShopDomain(true).__PS_BASE_URI__.'modules/zgallery/zgallery-photo.php?i='.$value['id_zgallery_album'].'&v='.$value['id_zgallery_photo'];
			}
			$result[$key]['linkImage'] = __PS_BASE_URI__.'modules/zgallery/img/photo/'.$value['image'];
			
			$x = strtotime ($result[$key]['date_add']);
			$result[$key]['date_add'] = date("F j, Y",$x);
			if ($id_customer!=0){
				if (Zphoto::isCustomerLike($result[$key]['id_zgallery_photo'],$id_customer)==0) {
					if ($result[$key]['limit_like']!=0 && ($result[$key]['nb_like'] >= $result[$key]['limit_like']))
						$result[$key]['allowLike'] = 3;
					else 
						$result[$key]['allowLike'] = 1;
				} else 
					$result[$key]['allowLike'] = 0;
			}
			else 
				$result[$key]['allowLike'] = 4;
			
			if ($result[$key]['allow_like']==0)
				$result[$key]['allowLike'] = 2;
		}
		
		return $result;
	}
	
	public static function getAlbum($id_lang = false, $active = true, $order = true, $sql_filter = '', $sql_sort = '',$sql_limit = '')
	{
		
		$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS('
			SELECT *
			FROM `'._DB_PREFIX_.'zgallery_album` c
			LEFT JOIN `'._DB_PREFIX_.'zgallery_album_lang` cl ON c.`id_zgallery_album` = cl.`id_zgallery_album`
			WHERE 1 '.$sql_filter.' '.($id_lang ? 'AND `id_lang` = '.(int)($id_lang) : '').' AND c.id_zgallery_album<>1
			'.($active ? 'AND `active` = 1' : '').'
			'.(!$id_lang ? 'GROUP BY c.id_zgallery_album' : '').'
			'.($sql_sort != '' ? $sql_sort : 'ORDER BY c.`level_depth` ASC, c.`position` ASC').'
			'.($sql_limit != '' ? $sql_limit : '')
		);
		return $result;
	}
	
	public static function recurseAlbum($albums,$id_selected = 1)
	{
		foreach ($albums as $album) {
			echo '<option value="'.$album['id_zgallery_album'].'"'.(($album['id_zgallery_album'] == $id_selected) ? ' selected="selected"' : '').'>'.$album['name'].'</option>';
		}
	}
	
	
	
	static public function getListPhotos ($id_zgallery_album,$id_lang, $active = true,  $sql_sort = '',$sql_limit = '') {
		global $cookie,$link;
		$linkHome = $link->getPageLink('');
		$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS('SELECT * FROM '._DB_PREFIX_.'zgallery_photo a
					LEFT JOIN '._DB_PREFIX_.'zgallery_photo_lang b ON (a.id_zgallery_photo=b.id_zgallery_photo )
					WHERE 
						a.id_zgallery_album='.$id_zgallery_album.' AND b.`id_lang` = '.(int)($id_lang).'
						'.($active ? 'AND `active` = 1' : '').'
						'.($sql_sort != '' ? $sql_sort : 'ORDER BY a.`date_add` ASC').''
		);
		foreach ($result as $key => $value){
			$result[$key]['linkImage'] = __PS_BASE_URI__.'modules/zgallery/img/photo/'.$value['image'];
			if (Configuration::get('PS_REWRITING_SETTINGS')) {
				$result[$key]['link'] = Zphoto::getLinkUrlPhoto ($value['id_zgallery_album'],$value['id_zgallery_photo'] );
			}else {
				$result[$key]['link'] = Tools::getShopDomain(true).__PS_BASE_URI__.'modules/zgallery/zgallery-photo.php?i='.$value['id_zgallery_album'].'&v='.$value['id_zgallery_photo'];
			}
		}
		return $result;
	}
	
	static public function  getPhoto ($id_zgallery_photo,$result) {
		foreach ($result as $key => $value){
			if ($value['id_zgallery_photo']==$id_zgallery_photo)
				return $key;
		}
	}
	static public function getNumberThumbs (){
		
	}
	
	public  function getLinkUrlP () {
		global $cookie,$link;
		$linkHome = $link->getPageLink('');
		if ( isset($_GET['i'])==0 ) {
			$i=1;
		}
		else $i=$_GET['i'];
		$frl = $this->getParentsAlbum ($i);
		$frl=$frl['freurl'];
		$out = $linkHome."album-zgallery";
		$out.='/'.$i.'_'."$frl";
		return $out;
	} 
	
	public  function getLinkUrlPhoto ($i=null,$v=null) {
		global $cookie,$link;
		$linkHome = $link->getPageLink('');
		
		if ($i==NULL)
			if ( isset($_GET['i'])==0 ) {
				$i=1;
			}
			else $i=$_GET['i'];
		if ($v==NULL) {
			if ( isset($_GET['v'])==0 ) {
				$v=1;
			}
			else $v=$_GET['v'];
		}
		$frl = Zphoto::getParentsAlbum ($i);
		$frl=$frl['freurl'];
		$out = $linkHome."photo-zgallery";
		$out.='/'.$i.'_'."$frl";
		$frl = Zphoto::getPhotoCurrent($v) ;
		$frl=$frl['freurl'];
		$out.='/'.$v.'_'."$frl";
		return $out;
	}
	
	static public function goPage($url, $pThumb)
	{
		return $url.($pThumb == 1 ? '' : (!strstr($url, '?') ? '?' : '&amp;').'pThumb='.(int)($pThumb));
	}
	
	static public function goLink($url, $pThumb)
	{
		if (Configuration::get('zgallery_photo_MODE')==1) 
			return $url.($pThumb == 1 ? '' : (!strstr($url, '?') ? '?' : '&amp;').'mode='.(int)($pThumb));
		else 
			return $url.($pThumb == 0 ? '' : (!strstr($url, '?') ? '?' : '&amp;').'mode='.(int)($pThumb));
	}
	
	
	
	static public function getPageLink($filename, $ssl = false, $id_lang = NULL)
	{
		global $cookie,$cache;
		if ($id_lang == NULL)
			$id_lang = (int)($cookie->id_lang);

		if ($this->allow == 1)
		{
			$url_rewrite = '';
			if ($filename != 'index.php')
			{
				$pagename = substr($filename, 0, -4);
				$url_rewrite = Db::getInstance()->getValue('
				SELECT url_rewrite
				FROM `'._DB_PREFIX_.'meta` m
				LEFT JOIN `'._DB_PREFIX_.'meta_lang` ml ON (m.id_meta = ml.id_meta)
				WHERE id_lang = '.(int)($id_lang).' AND `page` = \''.pSQL($pagename).'\'');
				$uri_path = $this->getLangLink((int)$id_lang).($url_rewrite ? $url_rewrite : $filename);
			}
			else
				$uri_path = $this->getLangLink((int)$id_lang);
		}
		else
		{
			$uri_path = '';
			if ($filename != 'index.php')
				$uri_path = $filename;
		}
	
		return (($ssl AND Configuration::get('PS_SSL_ENABLED')) ? Tools::getShopDomainSsl(true) : Tools::getShopDomain(true)).__PS_BASE_URI__.ltrim($uri_path, '/');
	}
	
	static public function goPageThumb($url, $nThumb)
	{
		return $url.(!strstr($url, '?') ? '?' : '&amp;').'nThumb='.(int)($nThumb);
	}
	
	public function getPaginationLinkPageNThumb($type, $id_object, $nb = false, $sort = false, $pagination = false, $array = false)
	{
		global $cookie,$link;
		$linkHome = $link->getPageLink('');
		if ($type AND $id_object)
			$url = $this->{'get'.$type.'Link'}($id_object, NULL);
		else
		{
			$url = $this->url;
			if (Configuration::get('PS_REWRITING_SETTINGS'))
				$url = $this->getLinkUrlP();
		}
		$vars = (!$array ? '' : array());
		$varsNb = array('nThumb', 'search_query');
		$varsSort = array('orderby', 'orderway');
		$varsPagination = array('pThumb','nThumb');
		$n = 0;
		foreach ($_GET AS $k => $value)
			if ($k != 'id_'.$type)
			{
				if (Configuration::get('PS_REWRITING_SETTINGS') AND ($k == 'isolang' OR $k == '_php?isolang' OR $k == 'id_lang' OR $k=='i'))
					continue;
				$ifNb = (!$nb OR ($nb AND !in_array($k, $varsNb)));
				$ifSort = (!$sort OR ($sort AND !in_array($k, $varsSort)));
				$ifPagination = (!$pagination OR ($pagination AND !in_array($k, $varsPagination)));
				if ($ifNb AND $ifSort AND $ifPagination AND !is_array($value))
					!$array ? ($vars .= ((!$n++ AND ($this->allow == 1 OR $url == $this->url)) ? '?' : '&').urlencode($k).'='.urlencode($value)) : ($vars[urlencode($k)] = urlencode($value));
			}
		if (!$array)
			return $url.$vars;
		$vars['requestUrl'] = $url;
		if ($type AND $id_object)
			$vars['id_'.$type] = (is_object($id_object) ? (int)$id_object->id : (int)$id_object);
		return $vars;
	}
	
	public function getPaginationLink($type, $id_object, $nb = false, $sort = false, $pagination = false, $array = false)
	{
		global $cookie,$link;
		$linkHome = $link->getPageLink('');
		if ($type AND $id_object)
			$url = $this->{'get'.$type.'Link'}($id_object, NULL);
		else
		{
			$url = $this->url;
			if (Configuration::get('PS_REWRITING_SETTINGS'))
				$url = $this->getLinkUrlP();
		}
		$vars = (!$array ? '' : array());
		$varsNb = array('nThumb', 'search_query');
		$varsSort = array('orderby', 'orderway');
		$varsPagination = array('pThumb');
		$n = 0;
		foreach ($_GET AS $k => $value)
			if ($k != 'id_'.$type)
			{
				if (Configuration::get('PS_REWRITING_SETTINGS') AND ($k == 'isolang' OR $k == '_php?isolang' OR $k == 'id_lang' OR $k=='i'))
					continue;
				$ifNb = (!$nb OR ($nb AND !in_array($k, $varsNb)));
				$ifSort = (!$sort OR ($sort AND !in_array($k, $varsSort)));
				$ifPagination = (!$pagination OR ($pagination AND !in_array($k, $varsPagination)));
				if ($ifNb AND $ifSort AND $ifPagination AND !is_array($value))
					!$array ? ($vars .= ((!$n++ AND ($this->allow == 1 OR $url == $this->url)) ? '?' : '&').urlencode($k).'='.urlencode($value)) : ($vars[urlencode($k)] = urlencode($value));
			}
		if (!$array)
			return $url.$vars;
		$vars['requestUrl'] = $url;
		if ($type AND $id_object)
			$vars['id_'.$type] = (is_object($id_object) ? (int)$id_object->id : (int)$id_object);
		return $vars;
	}
	
	public function getLink($type, $id_object, $nb = false, $sort = false, $pagination = false, $array = false)
	{
		global $cookie,$link;
		$linkHome = $link->getPageLink('');
		if ($type AND $id_object)
			$url = $this->{'get'.$type.'Link'}($id_object, NULL);
		else
		{
			$url = $this->url;
			if (Configuration::get('PS_REWRITING_SETTINGS')) {
				$url = $this->getLinkUrlP();
			}
		}
		$vars = (!$array ? '' : array());
		$varsNb = array('mode', 'search_query');
		$varsSort = array('orderby', 'orderway');
		$varsPagination = array('mode');

		$n = 0;
		foreach ($_GET AS $k => $value)
			if ($k != 'id_'.$type)
			{
				if (Configuration::get('PS_REWRITING_SETTINGS') AND ($k == 'isolang' OR $k == 'id_lang' OR $k=='i' OR $k == '_php?isolang'))
					continue;
				$ifNb = (!$nb OR ($nb AND !in_array($k, $varsNb)));
				$ifSort = (!$sort OR ($sort AND !in_array($k, $varsSort)));
				$ifPagination = (!$pagination OR ($pagination AND !in_array($k, $varsPagination)));
				if ($ifNb AND $ifSort AND $ifPagination AND !is_array($value))
					!$array ? ($vars .= ((!$n++ AND ($this->allow == 1 OR $url == $this->url)) ? '?' : '&').urlencode($k).'='.urlencode($value)) : ($vars[urlencode($k)] = urlencode($value));
			}
		if (!$array)
			return $url.$vars;
		$vars['requestUrl'] = $url;
		if ($type AND $id_object)
			$vars['id_'.$type] = (is_object($id_object) ? (int)$id_object->id : (int)$id_object);
		return $vars;
	}
	
	static public function getNamePhoto ($id){
		if ($id!=NULL) {
			$query = '
				SELECT image FROM `'._DB_PREFIX_.'zgallery_photo` 
					WHERE `id_zgallery_photo`='.$id.'
			';
			$results = Db::getInstance()->getRow($query);
			return $results['image'];
		}
		else return '';
	}
	
	static public function getParrent ($id=0){
		$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow('
			SELECT * FROM '._DB_PREFIX_.'zgallery_photo
			WHERE id_zgallery_photo='.$id.' 
		');
		return (int)($result['id_zgallery_album']);
	}
	
	static public function getPath ($id) {
		$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow('
			SELECT * FROM '._DB_PREFIX_.'zgallery_photo
			WHERE id_zgallery_photo='.$id.' 
		');
		return $rs;
	}
	
	protected function getLangLink($id_lang = NULL)
	{
		if (!$this->allow OR Language::countActiveLanguages() <= 1)
			return '';
			
		global $cookie;
		if (!$id_lang)
			$id_lang = (int)$cookie->id_lang;
		
		return Language::getIsoById((int)$id_lang).'/';
	}
	static public function getListNewPhotos($nb,$id_lang) {
		global $cookie,$link;
		$linkHome = $link->getPageLink('');
		
		$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS('
			SELECT * FROM '._DB_PREFIX_.'zgallery_photo a
			LEFT JOIN '._DB_PREFIX_.'zgallery_photo_lang b ON (a.id_zgallery_photo=b.id_zgallery_photo)
			WHERE a.active=1 AND b.id_lang='.$id_lang.'
			ORDER BY a.date_add DESC
			LIMIT 0,'.$nb.' 
		');
		foreach ($result as $key => $value){
			$result[$key]['linkImage'] = __PS_BASE_URI__.'modules/zgallery/img/photo/'.$value['image'];
			if (Configuration::get('PS_REWRITING_SETTINGS')) {
				$result[$key]['link'] = Zphoto::getLinkUrlPhoto ($value['id_zgallery_album'],$value['id_zgallery_photo'] );
			}else {
				$result[$key]['link'] = Tools::getShopDomain(true).__PS_BASE_URI__.'modules/zgallery/zgallery-photo.php?i='.$value['id_zgallery_album'].'&v='.$value['id_zgallery_photo'];
			}
		}
		return $result;
	}
	
	static public function getPhotoCurrent ($id) {
		global $cookie;
		$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow('
			SELECT * FROM '._DB_PREFIX_.'zgallery_photo a
			LEFT JOIN '._DB_PREFIX_.'zgallery_photo_lang b ON (a.id_zgallery_photo=b.id_zgallery_photo)
			WHERE a.id_zgallery_photo='.$id.' AND b.id_lang='.$cookie->id_lang.'
		');
		return $result;
	}
	
	static public function getParentsAlbum ($id) {
		global $cookie;
		$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow('
			SELECT * FROM '._DB_PREFIX_.'zgallery_album a
			LEFT JOIN '._DB_PREFIX_.'zgallery_album_lang b ON (a.id_zgallery_album=b.id_zgallery_album AND b.id_lang='.$cookie->id_lang.')
			WHERE a.id_zgallery_album='.$id.' 
		');
		return $result;
	}
	
	static public function copyFromPost()
	{
		/* Classical fields */
		foreach ($_POST AS $key => $value)
			if (key_exists($key, $this) AND $key != 'id_'.$this->table)
				$this->{$key} = $value;
//		echo $this->freurl; die ();
		
				
//		foreach ($this->fieldsValidateLang AS $key => $value)	{
//			$languages = Language::getLanguages(false);
//			foreach ($languages AS $language) {
//				if (isset($_POST[$key.'_'.(int)($language['id_lang'])]))
//					$this->{$key}[(int)($language['id_lang'])] = $_POST[$key.'_'.(int)($language['id_lang'])];
//			}
//		}
	}
	
	static public function getImageName ($id ) {
		$rs = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow('
			SELECT * FROM '._DB_PREFIX_.'zgallery_photo a
			WHERE a.id_zgallery_photo='.$id.' 
		');
		return $rs['image'];
	}
	
	/**
	 * Delete several objects from database
	 *
	 * return boolean Deletion result
	 */
	public function deleteSelection($selection)
	{
		if (!is_array($selection) OR !Validate::isTableOrIdentifier($this->identifier) OR !Validate::isTableOrIdentifier($this->table))
			die(Tools::displayError());
		$result = true;
		foreach ($selection AS $id)
		{
			$this->id = (int)($id);
			$name = Zphoto::getImageName ($id);
			Zphoto::_deleteImage($id,$name);
			$result = $result AND $this->delete();
		}
		return $result;
	}
	
	static public function _deleteImage($id,$name)
	{
		$target = dirname(__FILE__).'/../img/photo/'.$name;
		if (file_exists($target) AND !unlink($target))
			return false;
		return true;
	}
	
	static public function isCustomerLike ($id,$id_customer) {
		$query = '
			SELECT * FROM '._DB_PREFIX_.'zgallery_photo_like WHERE id_customer='.$id_customer.' AND id_zgallery_photo='.$id.'
		';
		$results = Db::getInstance()->ExecuteS($query);
		if (sizeof($results)==0)
			return 0;
		else return 1;
	}
	static public function getInfoPicture ($id,$id_customer) {
		$query = '
			SELECT nb_like,nb_view,allow_like,limit_like,id_zgallery_photo,random_lucky_member FROM '._DB_PREFIX_.'zgallery_photo WHERE id_zgallery_photo='.$id.'
		';
		$rs = Db::getInstance()->getRow($query);
		if ($id_customer!=0){
			if (Zphoto::isCustomerLike($rs['id_zgallery_photo'],$id_customer)==0) {
				if ($rs['limit_like']!=0 && $rs['nb_like'] >= $rs['limit_like'])
					$rs['allowLike'] = 2;
				else 
					$rs['allowLike'] = 1;
			} else 
				$rs['allowLike'] = 0;
		}
		else 
			$rs['allowLike'] = 2;
			
		if ($rs['allow_like']==0)
			$rs['allowLike'] = 2;
		$rs['allow_like'] = $rs['allowLike']; 
		return $rs;		
	}
	/**
	 * Set lucky member for customer when click like
	 * @param unknown_type $id
	 * @param unknown_type $lucky
	 */
	static public function randomLuckyMember ($id,$lucky,$id_photo) {
		$ran = rand (1,100);
		$date = date('Y-m-d H:i:s');
		if ($ran<=$lucky) {
			$rs = Db::getInstance()->getRow('
				SELECT * FROM '._DB_PREFIX_.'zgallery_lucky_member
				WHERE id_customer='.$id.'
			');
			if ($rs==null) {
				Db::getInstance()->Execute ('
					INSERT INTO `'._DB_PREFIX_.'zgallery_lucky_member` (`id_customer`,`id_photo`,`date_add`) VALUES ("'.$id.'","'.$id_photo.'","'.$date.'")
				');
				return true;
			}
		}
		return false;
	}
	/**
	 * get list member lucky
	 */
	static public function getMembersLucky () {
		return Db::getInstance()->ExecuteS('
			SELECT a.id_customer,CONCAT(b.firstname," ",b.lastname) as name FROM '._DB_PREFIX_.'zgallery_lucky_member a
			LEFT JOIN '._DB_PREFIX_.'customer b ON (a.id_customer=b.id_customer)
		');
	}
	
	static public function increaseViewPhoto ($id,$id_album) {
		$ip =  $_SERVER['REMOTE_ADDR'];
		$rs = Db::getInstance()->getRow('
				SELECT * FROM '._DB_PREFIX_.'zgallery_view
				WHERE id_zgallery_photo='.$id.' AND ip_client = "'.$ip.'"
			');
		if ($rs==null) {
			Db::getInstance()->ExecuteS('
				UPDATE `'._DB_PREFIX_.'zgallery_photo` 
				SET `nb_view`=(`nb_view`+1) 
				WHERE `id_zgallery_photo`='.$id.'
			');
			
			Db::getInstance()->ExecuteS('
				UPDATE `'._DB_PREFIX_.'zgallery_album` 
				SET `nb_view`=(`nb_view`+1) 
				WHERE `id_zgallery_album`='.$id_album.'
			');
			
			Db::getInstance()->ExecuteS('
				INSERT INTO `'._DB_PREFIX_.'zgallery_view` (`ip_client`, `id_zgallery_photo`) VALUES ("'.$ip.'", '.$id.')
			');
			
			return true;
		}
		return false;
	}
}
	


